# -*- coding: utf-8 -*-
"""
Created on Fri Mar 15 12:53:35 2013

@author: Henrique
"""

import random
import pygame

class scenery:
    
    def __init__(self, parent, color, s_texture):
        
        self.step = 5
        self.parent = parent
        self.tex_pos = 0
        
        self.surface_texture = s_texture
        self.terrain_start = self.parent.get_height()-self.parent.get_height()/4
        
        self.points = self.make_terrain()
        self.points_buffer = list()
        
        self.color = color
        
    def move(self):
        
        width = self.parent.get_width()
        height = self.parent.get_height()
        
        step = -1*self.step
        
        w = self.surface_texture.get_width()
        
        self.tex_pos = self.tex_pos + step

        if self.tex_pos <= -w:
            self.tex_pos = 0
        
        for i in range(len(self.points)):
            x = self.points[i][0]+step          
            self.points[i] = (x,self.points[i][1])
            
        while self.points[0][0] < 0:
            self.points.pop(0)

        dx = width - self.points[len(self.points)-1][0]
            
        if len(self.points_buffer) < dx:
            self.terrain_start = self.points[len(self.points)-1][1]
            self.points_buffer = self.make_terrain()
            
        for i in range(0,dx):
            self.points.append((width-(dx-i),self.points_buffer[0][1]))
            self.points_buffer.pop(0)

        for i in range(len(self.points_buffer)):
            x = self.points_buffer[i][0]+step          
            self.points_buffer[i] = (x,self.points_buffer[i][1])

    def make_terrain(self):
        
        itera = 10
        ran = 150
        H = 1

        points = [(0,self.terrain_start),(self.parent.get_width(),self.terrain_start)]

        for i in range(0,itera):
            p = list()
            
            for j in range(0,len(points)-1):
                
                x = (points[j][0]+points[j+1][0])/2
                y = (points[j][1]+points[j+1][1])/2+random.randint(-ran,ran)   
                
                p.append((j+1,x,y))
            
            for j in range(0,len(p)):
                points.insert(p[j][0]+j,(p[j][1],p[j][2]))
                
            ran = int(ran/(2**H))

        return points
        
    def draw(self):
        
        self.draw_skytex()
        self.draw_terrain()
    
    def draw_terrain(self):
        
        height = self.parent.get_height()
        width = self.parent.get_width()
        surface = self.parent.get_surface()
        
        points = self.points
        #for j in range(0,len(points)-1):
            #pygame.gfxdraw.textured_polygon(surface,(points[j],points[j+1],(points[j+1][0],height),(points[j][0],height)),self.surface_texture,self.tex_pos,0)
        pygame.gfxdraw.textured_polygon(surface,points+[(width,height),(0,height)],self.surface_texture,self.tex_pos,0)
    
    def draw_skytex(self):
        
        height = self.parent.get_height()
        width = self.parent.get_width()
        surface = self.parent.get_surface()
        
        tex = pygame.image.load("textures/moon_sky2.jpg").convert()
        
        pygame.gfxdraw.textured_polygon(surface,[(0,0),(width,0),(width,2*height),(0,2*height)],tex,self.tex_pos/20,20)
    
    def draw_sky(self):
        
        height = self.parent.get_height()
        width = self.parent.get_width()
        surface = self.parent.get_surface()
        
        di = height/100
        dc = 255/100
        db = 40/50
        r = 0
        g = 0
        b = 0
        
        i=0
        while i<height:
        
            pygame.draw.rect(surface,(r,g,b,255),(0,i,width,di))
            i = i+di
            r = r+dc
            g = g+dc
            b = b+dc
            
    def set_texture(self, texture):
        
        self.surface_texture = texture
    